package MobileCommServer;

import jssc.SerialPort;

/**
 * <pre>
 * @Package : MobileCommServer
 * @File    : SerialSender.java
 * <pre>
 *
 * @Author  : 차재우
 * @Date    : 2013. 7. 25.
 * @Version :
 */
class SerialSender implements Runnable {
	private SerialPort sp = null;
	private SerialQueue sendQueue = null;	

	/**
	 * <pre>
	 * @Comment : 생성자
	 * <pre>
	 *
	 * @param sp
	 */
	public SerialSender(SerialPort sp, SerialQueue sendQueue) {
		this.sp = sp;
		this.sendQueue = sendQueue;
	}

	/**
	 * <pre>
	 * @Comment : 시리얼 통신 전송 스레드
	 * <pre>
	 *
	 * @Method  : run
	 */
	public void run() {
		while(true) {
			try {
				if (this.sendQueue.IsEmpty() == true)
				{
					Thread.yield();
					continue;
				}
				
				PassData sendData = this.sendQueue.Pop();
				
				byte[] data = sendData.getBytePassData();
				sp.writeBytes(data);

				System.out.println("Data size       : " + data.length);
				System.out.println("Send queue size : " + sendQueue.GetQueueSize());
				System.out.println("=================================================================================\n");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
